package com.ssm.zyn.service.impl;

import com.ssm.zyn.dao.ClassesMapper;
import com.ssm.zyn.eo.ClassesEOpage;
import com.ssm.zyn.model.Classes;
import com.ssm.zyn.service.Classesservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class ClassesServiceImpl implements Classesservice {
    @Autowired
    private ClassesMapper classesMapper;

    /**
     *
     * @param classes
     * @return
     */
    @Override
    public int AddClasses(Classes classes) {
        int returnInt=0;
        /**1.业务的判断
         * (1)班级名称不能重复
         */
        //（1）判断班级名称
        int cName=classesMapper.getClassesNameCount(classes.getCname());
        if (cName<1){
            //2.数据的添加
            int addClassesInt = classesMapper.insertClasses(classes);


            //3.返回值的设置
            returnInt = addClassesInt;
        } else {
            returnInt = -1;//有相同的角色名称
        }

        return returnInt;


    }

    /**
     *
     * @param classes
     * @return
     */
    @Override
    public int UpdateClasses(Classes classes) {
        int isUpdate=0;
        int cName=classesMapper.getClassesNameCount(classes.getCname());
        try {
            if (cName<1){
                isUpdate = classesMapper.updateClasses(classes);
            }
        }catch (Exception e){
            isUpdate = -1;
            //打印更新时报错的情况
            System.out.println("更新角色信息时执行SQL报错的原因为"+e.getMessage());
        }
        return isUpdate;
    }

    /**
     *
     * @param classid
     * @return
     */
    @Override
    public int DeleteClasses(int classid) {
        int delClasses=0;
        try{
            delClasses=classesMapper.delClasses(classid);
        }catch (Exception e){
            delClasses=-1;
            System.out.println("删除班级信息时执行SQL报错，信息为"+e.getMessage());
        }
        return delClasses;
    }

    /**
     *
     * @param classesEOpage
     * @return
     */
    @Override
    public List<ClassesEOpage> getWhereAll(ClassesEOpage classesEOpage) {
        List<ClassesEOpage> listClasses= classesMapper.getWhereAll(classesEOpage);
        return listClasses;
    }
}
